package com.ytjj.qmyx.supplychain.common.enums;

import lombok.Getter;
import org.apache.logging.log4j.util.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
public enum KuaiDi100Enum {
    YUAN_TONG_FAST("圆通速递", "yuantong"),
    YUN_DA("韵达快递", "yunda"),
    YUN_DA_KUAI_YUN("韵达快运", "yundakuaiyun"),
    FENG_WANG("丰网速运", "fengwang"),
    ZHONG_TONG("中通快递", "zhongtong"),
    ZHONG_TONG_KUAI_YUN("中通快运", "happylink"),
    SHENG_TONG("申通快递", "shentong"),
    JI_TU("极兔速递", "jtexpress"),
    ZHONG_TONG_GUO_NEI("邮政快递包裹", "youzhengguonei"),
    EMS("EMS", "ems"),
    YOU_ZHENG_BK("邮政标准快递", "youzhengbk"),
    EMS_GUO_JI("EMS-国际件", "emsguoji"),
    SUN_FENG_FAST("顺丰速运", "shunfeng"),
    SUN_FENG_KUAI_YUN("顺丰快运", "shunfengkuaiyun"),
    JING_DONG("京东物流", "jd"),
    JING_DONG_KUAI_YUN("京东快运", "jingdongkuaiyun"),
    JIE_BANG_KUAIDI("德邦快递", "debangkuaidi"),
    JIE_BANG("德邦", "debangwuliu"),
    ZHONG_YOU_EX("众邮快递", "zhongyouex"),
    SU_TENG("速腾快递", "suteng"),
    ZAI_JI_SONG("宅急送", "zhaijisong"),
    BAI_SHI("百世快递", "huitongkuaidi"),
    BAI_SHI_KUAI_YUN("百世快运", "baishiwuliu"),
    ;

    /**
     * 快递中文名
     */
    private String expressName;

    /**
     * 快递编码
     */
    private String expressCode;

    KuaiDi100Enum(String expressName, String expressCode) {
        this.expressName = expressName;
        this.expressCode = expressCode;
    }

    /**
     * 获取快递编码
     *
     * @param expressName
     * @return
     */
    public static String getExpressCode(String expressName) {
        KuaiDi100Enum[] values = KuaiDi100Enum.values();
        for (KuaiDi100Enum expressEnum : values) {
            if(expressEnum.getExpressName().equals(expressName)) {
                return expressEnum.getExpressCode();
            }
        }
        return null;
    }



    public static String getExpressName(String expressCode){
        for (KuaiDi100Enum value : KuaiDi100Enum.values()) {
            if(value.getExpressCode().equals(expressCode)){
                return value.getExpressName();
            }
        }
        return null;
    }


    public static String getErrorMsg(String childOrderNo,String expressCode,String expressNo) throws Exception{
      String returnStr =  Strings.EMPTY;
        if (ZHONG_TONG_GUO_NEI.getExpressCode().equals(expressCode) || EMS.getExpressCode().equals(expressCode)
                || YOU_ZHENG_BK.getExpressCode().equals(expressCode) || EMS_GUO_JI.getExpressCode().equals(expressCode)) {
            String regex = "\\d{13}";
            Pattern pattern = Pattern.compile(regex);
            Matcher isNum = pattern.matcher(expressNo);
            if( !isNum.matches() ) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }

        } else if(SUN_FENG_FAST.getExpressCode().equals(expressCode) || SUN_FENG_KUAI_YUN.getExpressCode().equals(expressCode)) {
            if (expressNo.length()<=2 || !"SF".equals(expressNo.substring(0,2))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(JING_DONG.getExpressCode().equals(expressCode) || JING_DONG_KUAI_YUN.getExpressCode().equals(expressCode)) {
            if (expressNo.length()<=2 || !"JD".equals(expressNo.substring(0,2))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(JIE_BANG_KUAIDI.getExpressCode().equals(expressCode) || JIE_BANG.getExpressCode().equals(expressCode)) {
            if (expressNo.length()<=3 || !"DPK".equals(expressNo.substring(0,3))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(ZHONG_YOU_EX.getExpressCode().equals(expressCode) ) {
            if (expressNo.length()<=2 || !"ZY".equals(expressNo.substring(0,2))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(SU_TENG.getExpressCode().equals(expressCode) ) {
            String regex = "\\d{12}";
            Pattern pattern = Pattern.compile(regex);
            Matcher isNum = pattern.matcher(expressNo);
            if( !isNum.matches() || !"88".equals(expressNo.substring(0,2))){
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(ZAI_JI_SONG.getExpressCode().equals(expressCode) ) {
            if (expressNo.length()<=3 || !"ZJS".equals(expressNo.substring(0,3))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(BAI_SHI.getExpressCode().equals(expressCode) || BAI_SHI_KUAI_YUN.getExpressCode().equals(expressCode)) {
            String regex = "\\d{11}";
            Pattern pattern = Pattern.compile(regex);
            Matcher isNum = pattern.matcher(expressNo);
            if( !isNum.matches() || (expressNo.length()<=2 || !"81".equals(expressNo.substring(0,2)))){
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(YUAN_TONG_FAST.getExpressCode().equals(expressCode) ) {
            if (expressNo.length()<=2 || !"YT".equals(expressNo.substring(0,2))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(YUN_DA.getExpressCode().equals(expressCode) || YUN_DA_KUAI_YUN.getExpressCode().equals(expressCode)) {
            String regex = "\\d{12}";
            Pattern pattern = Pattern.compile(regex);
            Matcher isNum = pattern.matcher(expressNo);
            if( !isNum.matches()){
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(FENG_WANG.getExpressCode().equals(expressCode) ) {
            if ( expressNo.length() != 15 || !"99".equals(expressNo.substring(0,2))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(ZHONG_TONG.getExpressCode().equals(expressCode) || ZHONG_TONG_KUAI_YUN.getExpressCode().equals(expressCode) ) {
            if (expressNo.length() == 14 && ("78".equals(expressNo.substring(0,2)) ||"75".equals(expressNo.substring(0,2))) ) {
            } else {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }

        } else if(SHENG_TONG.getExpressCode().equals(expressCode) ) {
            if ( expressNo.length() != 15 || !"7".equals(expressNo.substring(0,1))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        } else if(JI_TU.getExpressCode().equals(expressCode) ) {
            if ( expressNo.length() <=2 || !"JT".equals(expressNo.substring(0,2))) {
                returnStr= String.format("系统检测到:您的订单%s 录入的快递单号 %s 和快递公司 %s 可能不匹配，是否继续？",childOrderNo,expressNo,getExpressName(expressCode));
            }
        }
        return returnStr;
    }
    

}
